﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.BusinessLogic;
using Module.Core.Common;
using Module.Core.EntitiesInfo;
using Module.Core.EntitiesInfo.TablesInfo;
using Module.Core.LoginSystem.Commons.Framwork;

namespace Web.Cms.City
{
    public partial class CityManager : AdminPage
    {
        #region Khai báo
        private int cityId = 0;
        #endregion

        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                cityId = ObjConvertUtility.ToInt32(Request.QueryString["cityId"]);
                if (!Page.IsPostBack)
                {
                    txtCreateOn.SelectedDate = DateTime.Now;
                    LoadData();

                }
            }
            catch (Exception ex)
            {
            }
        }
        #endregion

        #region LoadData
        private void LoadData()
        {
            try
            {
                if (cityId > 0)
                {
                    btnUpdate.Visible = true;
                    btnAdd.Visible = false;
                    CityInfo info = CityBL.Select(cityId);
                    if (info != null)
                    {
                        txtCityName.Text = info.CityName;
                        //txtCreateBy.Text = info.CreatedBy;
                        dropPriority.SelectedValue = info.Priority.ToString();
                        txtCreateOn.SelectedDate = info.CreatedOn;
                    }
                }
                else
                {
                    btnUpdate.Visible = false;
                    btnAdd.Visible = true;
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region FillData
        void FillData(CityInfo info)
        {
            info.CityName = txtCityName.Text;
            info.CreatedBy = CurrentUser.Username;
            info.CreatedOn = DateTime.Now;
            info.CityId = cityId;
            info.IsActive = true;
            info.Priority = int.Parse(dropPriority.SelectedValue);


        }
        #endregion


        #region thêm mới
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                CityInfo info = new CityInfo();
                FillData(info);
                int i = CityBL.Insert(info);
                if (i > 0)
                {
                    lblUpdateStatus.Text = "Bạn đã thêm mới thành công";
                    ClearText();
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region Chỉnh sửa
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                CityInfo info = new CityInfo();
                FillData(info);
                CityBL.Update(info);
                lblUpdateStatus.Text = "Bạn đã chỉnh sửa thành công";
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion


        #region ClearText
        void ClearText()
        {
            txtCityName.Text = "";
            //txtCreateBy.Text = "";
        }
        #endregion
    }
}



